package com.example.stardemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.DisplayMetrics;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class ParallaxDecoration extends RecyclerView.ItemDecoration {

    private List<Bitmap> bitmapList = new ArrayList<>();//图源
    private final Context context;
    private int bitmapHeight;//缩放后的位图高
    private float parallax = 0.7f;


    /**
     * 构造器
     */
    public ParallaxDecoration(Context context) {
        this.context = context;
    }


    /**
     * 根据资源数组生成位图
     */
    public void decodeBitmap(int[] bitmapResources) {
        List<Bitmap> bitmapList = new ArrayList<>();
        BitmapFactory.Options options = new BitmapFactory.Options();
        for (int bitmapResource : bitmapResources) {
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), bitmapResource, options);
            Bitmap scaledBitmap = scaleBitmap(bitmap);
            bitmapList.add(scaledBitmap);
        }
        if (bitmapList.size() != 0) {
            bitmapHeight = bitmapList.get(0).getHeight();
            //int bitmapWidth = bitmapList.get(0).getWidth();
        }
        this.bitmapList = bitmapList;
    }


    /**
     * 将位图缩放为适配屏幕的大小
     */
    private Bitmap scaleBitmap(Bitmap bitmap) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int screenWidthPixels = dm.widthPixels;
        float s = (float) screenWidthPixels / bitmap.getWidth();
        Matrix matrix = new Matrix();
        matrix.postScale(s, s);//长宽缩放比例
        Bitmap resizeBmp;
        resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizeBmp;
    }


    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (!bitmapList.isEmpty()) {
            //根据画布宽高得到屏幕高
            int screenHeight = c.getHeight();
            //获取在屏幕中完整显示的张数
            int allInScreen = screenHeight / bitmapHeight;
            //判断除去完整显示的图片，屏幕剩余的高度是否大于1像素，如果不足1像素，说明只需要多画一张
            boolean outOfScreen = (screenHeight % bitmapHeight) > 1;
            //计算可能显示的最多图片数量
            int maxVisibleCount = outOfScreen ? allInScreen + 2 : allInScreen + 1;
            //整体背景滑出屏幕的长度
            RecyclerView.LayoutManager lm = parent.getLayoutManager();
            if(lm != null){
                float parallaxOffset = lm.computeVerticalScrollOffset(state) * parallax;
                //第一张可见图片在list中的索引
                int firstVisible = (int) (parallaxOffset / bitmapHeight);
                //第一张可见图片在屏幕外的长度
                float firstVisibleOffset = parallaxOffset % bitmapHeight;
                //最优的要画的图片数
                int bestDrawCount = (int) firstVisibleOffset == 0 ? maxVisibleCount - 1 : maxVisibleCount;
                //开始绘制
                c.save();
                c.translate(0f, -firstVisibleOffset);
                for (int i = firstVisible; i < firstVisible + bestDrawCount; i++) {
                    c.drawBitmap(
                            bitmapList.get((i) % bitmapList.size()),//这一步很关键，通过取余本质上实现了循环的功能
                            0f,
                            (i - firstVisible) * bitmapHeight,
                            null
                    );
                }
                c.restore();
            }
        }
    }


    /**
     * 设置背景视差系数，默认为0.7
     */
    public void setParallax(float parallax) {
        if (parallax > 0 && parallax < 1) {
            this.parallax = parallax;
        }
    }


}
